﻿using System;
using System.Data;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.OMS;
using Me.Amon.POS.C;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class UpdateBrand : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_01)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var dba = new RDBEngine();
            ListBrand(dba, user.CorpId);
        }

        #region 事件处理
        protected void LbBrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            ReadBrand(new RDBEngine(), LbBrand.SelectedValue);
        }

        protected void BtUpdate_Click(object sender, EventArgs e)
        {
            var user = WebUtil.CurrentUser(Session);

            var dba = new RDBEngine();
            SaveBrand(dba, user.CorpId);

            dba.Reset();
            ListBrand(dba, user.CorpId);
        }
        #endregion

        private void ListBrand(RDBEngine dba, string corp)
        {
            dba.AddTable(DSpuBrand.TABLE);
            dba.AddColumn(DSpuBrand.ID);
            dba.AddColumn(DSpuBrand.NAME);
            dba.AddWhere(DSpuBrand.CORP_ID, corp);
            //dba.AddWhere(DSpuBrand.STATUS, CSpuBrand.STATUE_ENABLED.ToString(), false);

            var dt = dba.ExecuteSelect();
            LbBrand.DataSource = dt;
            LbBrand.DataValueField = DSpuBrand.ID;
            LbBrand.DataTextField = DSpuBrand.NAME;
            LbBrand.DataBind();
        }

        private void ReadBrand(RDBEngine dba, string vip)
        {
            dba.AddTable(DSpuBrand.TABLE);
            dba.AddWhere(DSpuBrand.ID, vip);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 1)
            {
                return;
            }

            DataRow row = dt.Rows[0];
            HdId.Value = vip;
            TbName.Text = "" + row[DSpuBrand.NAME];
            CbStatus.Checked = CSpuBrand.STATUS_DISABLED == (int)row[DSpuBrand.STATUS];
        }

        private void SaveBrand(RDBEngine dba, string corp)
        {
            var name = TbName.Text.Trim();
            if (!WebUtil.IsValid(name, "品牌名称", CSpuBrand.NAME, LlErr))
            {
                return;
            }

            var id = HdId.Value;
            var user = WebUtil.CurrentUser(Session);

            dba.AddTable(DSpuBrand.TABLE);
            dba.AddParam(DSpuBrand.CORP_ID, corp);
            dba.AddParam(DSpuBrand.CODE, "");
            dba.AddParam(DSpuBrand.NAME, name);
            dba.AddParam(DSpuBrand.STATUS, CbStatus.Checked ? CSpuBrand.STATUS_DISABLED : CSpuBrand.STATUE_ENABLED);
            dba.AddParam(DSpuBrand.UPDATE_TIME, DSpuBrand.NOW, false);

            if (CharUtil.IsValidateHash(id))
            {
                dba.AddWhere(DSpuBrand.ID, id);
                dba.ExecuteUpdate();
            }
            else
            {
                dba.AddParam(DSpuBrand.ID, HashUtil.UtcTimeInHex());
                dba.AddParam(DSpuBrand.CREATE_TIME, DSpuBrand.NOW, false);
                dba.ExecuteInsert();
            }

            ShowDef();
        }

        private void ShowDef()
        {
            HdId.Value = "";
            TbName.Text = "";
        }
    }
}